/**************************************************************************************************
* Title: SmurfsEntity.cpp
* Author: Gael Huber
* Description: Describes a game object, which contains linkages to various other components in
*	the engine and manages its relation to those components. 
**************************************************************************************************/
#include "SmurfsEntity.h"

UINT64 Entity::nextAvailableID = 0;

/**************************************************************************************************
* Constructor
**************************************************************************************************/
Entity::Entity(void) {
	mesh = new Mesh();	// Initialize
	rigidBody = new RigidBody();	// Create the object's rigid body
	assignNextID();	// Assign the object the next available unique ID
}

/**************************************************************************************************
* Constructor
*
* Mesh* mesh	Mesh that coincides to the entity
**************************************************************************************************/
Entity::Entity(Mesh* mesh, RigidBody* body) {
	this->mesh = mesh;	// Assign the mesh

	// Create the object's rigid body
	rigidBody = body;	

	assignNextID();	// Assign the object the next available unique ID
}

/**************************************************************************************************
* Destructor
**************************************************************************************************/
Entity::~Entity(void) {
	// Delete primary components
	delete mesh;
	mesh = 0;

	delete rigidBody;
	rigidBody = 0;
}

/**************************************************************************************************
* Update that happens post-physics. This updates the mesh position and orientation based on the 
* rigid body.
**************************************************************************************************/
void Entity::postPhysicsUpdate(void) {
	mesh->screenCoords = rigidBody->position;
	//mesh->rotationAxis = rigidBody->axes;

	// Define start and end of vector
	std::vector<Vector3>::iterator begin = mesh->vertices.begin();
	std::vector<Vector3>::iterator end = mesh->vertices.end();

	// Iterate through the list and update each vertex
	Quaternion q = Quaternion(rigidBody->angle, rigidBody->axisOfRotation);
	for(std::vector<Vector3>::iterator itr = begin; itr != end; ++itr) {
		itr->rotate(q);
	}
}

/**************************************************************************************************
* Assign the next available unique ID
**************************************************************************************************/
void Entity::assignNextID(void) {
	uniqueID = ++nextAvailableID;
}


// return the rigid body (I know I shouldn't do this... but I want to modify things based on input...)
RigidBody* Entity::getBody(void) { return rigidBody; }